<?php 
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/users.php';
require_once '../../model/email.php';
include_once("../../model/trade.php");
include_once("../../model/state.php");
require_once "../../includes/classes/paging_htacess.php";

## creating objects of required models
$userObj  = new Model_Users();
$tradeObj = new Model_TRADE();
$stateObj = new Model_State();

if($_SESSION['user_type'] == '3')
{
	@header("location:".SITE_URL."/");
	exit;
}

/*===========================================================================*/
$userId =$_SESSION['getbids_id'];
if($userId!='')
{
	$userDetArray = $userObj->getUserDetailsByUserId($userId);
	$smarty->assign("userDetArray", $userDetArray);	
}
/*===========================================================================*/	


## for pagination
	$page = $_GET['id1'];
	if(!isset($_GET['id1']))
	$page = 1;
	else
	$page = $page;
	
## limit per pag 
$tetsimonialPerPage =10;
$StartRow = $tetsimonialPerPage  * ($page-1);
## Getting all jobs from database according to user
if($_POST['trade']!='0' && $_POST['trade']!='')
{
	$trade = $_POST['trade'];
} 
elseif($_GET['id2']!='' && $_GET['id2']!='trade')
{
	$trade =$_GET['id2']; 
}
elseif($_GET['tradename']!='')
{
	$getnewtradedetail = $tradeObj->checkTradeCatNameExits(str_replace("-"," ",str_replace("and","&",$_GET['tradename'])));	
	$trade = $getnewtradedetail['id'];	
}
else 
{
	$trade = '';
}

if($_POST['jobtype']!='0' && $_POST['jobtype']!='')
{
	$jobtype = $_POST['jobtype']; 
} 
elseif($_GET['id3']!='' && $_GET['id3']!='jobtype')
{
	$jobtype =$_GET['id3']; 
}
elseif($_GET['jobtypename']!='')
{
	$getnewtradedetail = $tradeObj->checkTradeCatNameExits(str_replace("-"," ",str_replace("and","&",$_GET['jobtypename'])));	
	$jobtype = $getnewtradedetail['id'];	
}
else
{
	$jobtype = '';
}


if($_POST['postalcode']!='')
{
	$postalcode = $_POST['postalcode']; 
}
elseif($_GET['id4']!='' && $_GET['id4']!='postalcode')
{
	$postalcode =$_GET['id4']; 
}
else 
{
	$postalcode = '';
}

if(($_GET['id2']=='' && $_GET['id3']=='' && $_GET['id4']=='') && ($_POST['trade']=='' && $_POST['jobtype']=='' && $_POST['postalcode']=='') && ($_GET['tradename']=='' && $_GET['jobtypename']==''))
{
	if($trade=='')
	{
		$trade = $userDetArray['tradeid']; 
	}
	if($postalcode=='')
	{
		$postalcode = $userDetArray['workarea']; 
	}
}
$smarty -> assign("trade",$trade);
$smarty -> assign("jobtype",$jobtype);
$smarty -> assign("postalcode",$postalcode);

$jobsArray  = $userObj->FindJobAccordingToAll($trade,$jobtype,$postalcode,'','',false,$userId);
$totalCount        = count($jobsArray);
$smarty -> assign("recordsFound",$totalCount);

$result = $userObj->FindJobAccordingToAll($trade,$jobtype,$postalcode,$tetsimonialPerPage,$StartRow,true,$userId);
$show = 1;
$total_pages = ceil($totalCount / $tetsimonialPerPage);
	if($total_pages > 1)
	$smarty -> assign("showpgnation","yes");
	$showing    = !isset($_GET["id1"]) ? 1 : $page;
	
	## it shoould be file name 
	if(($_GET['id2']!='' || $_GET['id3']!='' || $_GET['id4']!='' || $_GET['tradename']!='' || $_GET['jobtypename']!='') || ($_POST['trade']!='' || $_POST['jobtype']!='' || $_POST['postalcode']!=''))
	{
		$firstlink ='';
		
		if(($_POST['trade']!='0' || $_GET['id2']!='' || $_GET['tradename']!='' )&& $_GET['id2']!='trade')
			$firstlink  .= $trade."/";
		else	
			$firstlink  .= "trade/";

		if(($_POST['jobtype']!='0' || $_GET['id3']!='' || $_GET['jobtypename']!='')&& $_GET['id3']!='jobtype')
			$firstlink  .= $jobtype."/";
		else	
			$firstlink  .= "jobtype/";
			

		if(($_POST['postalcode']!='' || $_GET['id4']!='') && $_GET['id4']!='postalcode')
			$firstlink  .= $postalcode."/";
		else	
			$firstlink  .= "postalcode/";
			
		
		$firstlink .= "findjobs/";	
	}
	else
		$firstlink  = "findjobs/";
	
	$baselink   = $firstlink; 
	if($total_pages!=0)
		$pagenation = pagination($total_pages, $show, $showing, $firstlink, $baselink, "", "jobs");
	$smarty -> assign("pagenation", $pagenation);
	$smarty -> assign("page", $page);
	//
	
	for($i=0;$i<count($result);$i++)
	{
		if($result[$i]['trade_id']!='')
		{
			$gettradedetail = $tradeObj->getTradeCategoryDetailsById($result[$i]['trade_id']);
			$result[$i]['tradename'] = $gettradedetail['trade_category'];
		}
		
		if($result[$i]['jobtype_id']!='')
		{
			$getjobtypedetail = $tradeObj->getTradeCategoryDetailsById($result[$i]['jobtype_id']);
			$result[$i]['jobtypename'] = $getjobtypedetail['trade_category'];
		}
		
		if($result[$i]['area']!='')
		{
			$getstatedetail = $stateObj->getPostalCodeDetailsByPostalId($result[$i]['area']);
			$result[$i]['suburbname'] = $getstatedetail['suburb'];
		}
				
		if($result[$i]['postjob_id']!='')
		{
			$getcounttradesmaninterested = $userObj->getleadcountofTradesmanByJobId($result[$i]['postjob_id']);
			$result[$i]['counttradesmaninterested'] = $getcounttradesmaninterested['cnt'];
		}
		
		if($result[$i]['state_id']!=''){
		$State_array = $stateObj->getStateDetailsById($result[$i]['state_id']);
		$result[$i]['state_name'] = $State_array['state_name'];
		}
		
	}
	//echo"<pre>";print_R($result);die();
	$smarty->assign("jobsArray", $result);
	

$getallTradeCat = $tradeObj->getAllTradesCategoryWithActiveStatus();
$i=0;
foreach($getallTradeCat as $newgetallTradeCat)
{
	$new_tradename =str_replace(" ","-",$newgetallTradeCat['trade_category']);	
	$getallTradeCat[$i]['new_tradename'] =str_replace("&","and",$new_tradename);	
	$i++;
}
$smarty->assign("getallTradeCat", $getallTradeCat);

$getallTradeJobType = $tradeObj->getAllJobTypeWithActiveStatus();
$i=0;
foreach($getallTradeJobType as $newgetallTradeJobType)
{
	$new_tradename =str_replace(" ","-",$newgetallTradeJobType['trade_category']);	
	$getallTradeJobType[$i]['new_jobtypename']= str_replace("&","and",strtolower($new_tradename));	
	$i++;
}
$smarty->assign("getallTradeJobType", $getallTradeJobType);	



if($_SESSION['msg']){
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
}

$active ="findjobs";
$smarty->assign('active',$active);

$smarty->display(TEMPLATEDIR.'/modules/jobs/findjobs.tpl');
unset($smarty);
?>
